package org.example.myl.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@Aspect
public class LoggingAspect {

    @Pointcut("execution( *  org.example.myl.controller.StudentController.insert_student(..)))")
    public void logDeleteStudent() {}
    // 方法执行前记录请求参数
//    @Before("logDeleteStudent()")
//    public void logBefore(JoinPoint joinPoint) {
//        long start = System.currentTimeMillis();
//        // 获取方法参数
//        Object[] args = joinPoint.getArgs();
//        long executionTime = System.currentTimeMillis() - start;
//        log.info("insert_student方法执行完毕，耗时: {} ms", executionTime);
//        // 打印方法签名和参数
//        log.info("Before method: {}. Arguments: {}", joinPoint.getSignature(), args);
//    }
//
//    // 方法执行后记录返回信息（这里是 void 类型的方法，不返回值，但可以记录日志）
//    @AfterReturning("logDeleteStudent()")
//    public void logAfter(JoinPoint joinPoint) {
//        log.info("After method: {}. Execution completed.", joinPoint.getSignature());
//    }
    @Around("logDeleteStudent()")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object result;
        try {
            // 打印方法签名和参数
            log.info("Before method: {}. Arguments: {}", joinPoint.getSignature(), joinPoint.getArgs());
            result = joinPoint.proceed(); // 执行目标方法
        } catch (Throwable throwable) {
            log.error("Exception in " + joinPoint.getSignature().getName() + ". Message: " + throwable.getMessage());
            throw throwable; // 如果需要可以在这里处理异常或重新抛出
        }
        long executionTime = System.currentTimeMillis() - start;
        log.info("After method: {}. Execution completed in {} ms", joinPoint.getSignature(), executionTime);
        return result;
    }
}
